package com.android.zgx.msg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity {
	/*
	 * Variable Definition
	 */
	private TextView mResultText;
	private List<Map<String, Object>> mResultList;
	
	final String PHONE_NUMBER = "PhoneNumber";
	final String SHORTMSG_TIMES = "ShortMsgTimes";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
		readAllMsgLog(mResultList);
		showCallLog(mResultList);
	}

	// Initialize Control and Variable
	private void init() {
		mResultText = (TextView) findViewById(R.id.resultText);

		mResultList = new ArrayList<Map<String, Object>>();
	}

	/*
	 * Android Msg Log
	 * 
	 * @Param ResultList for in/out
	 */
	private void readAllMsgLog(List<Map<String, Object>> ResultList) {
		readSentMsgLog(ResultList);
		readInboxMsgLog(ResultList);
		readOutboxMsgLog(ResultList);
	}
	private void readSentMsgLog(List<Map<String, Object>> ResultList) {
		readMsgLog(ResultList,"content://sms/sent");
	}
	private void readInboxMsgLog(List<Map<String, Object>> ResultList) {
		readMsgLog(ResultList,"content://sms/inbox");
	}
	private void readOutboxMsgLog(List<Map<String, Object>> ResultList) {
		readMsgLog(ResultList,"content://sms/outbox");
	}
	/*
	 * Android Msg Log
	 * 
	 * @Param ResultList for in/out
	 */
	private void readMsgLog(List<Map<String, Object>> ResultList, String msgUri) {
		// Access to the Content
		ContentResolver cr = getContentResolver();

		// Query for Msg Log
		final Cursor cursor = cr.query(Uri.parse(msgUri), new String[]{"address","body"},
				null, null, "address desc");

		// Work with Records
		for (int i = 0; i < cursor.getCount(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();

			cursor.moveToPosition(i);
			String phoneNumber = cursor.getString(0);
			map.put(PHONE_NUMBER, phoneNumber);
			map.put(SHORTMSG_TIMES, 1);

			// if the PhoneNumber exists in the List
			for (Map<String, Object> listItem : ResultList) {
				if (listItem.get(PHONE_NUMBER).toString().equals(phoneNumber)) {
					Integer times = 1 + Integer.parseInt(listItem.get(
							SHORTMSG_TIMES).toString());
					map.put(SHORTMSG_TIMES, times);
					ResultList.remove(listItem);
					break;
				}
			}
			ResultList.add(0,map);
		}
	}
	
	/*
	 * Format Result and Show in the TextView
	 * 
	 * @Param ResultList for in
	 */
	private void showCallLog(List<Map<String, Object>> ResultList) {
		// if the PhoneNumber exists in the List
		String tmpStr = "";
		for (Map<String, Object> listItem : ResultList) {
			tmpStr += listItem.get(PHONE_NUMBER).toString() + ",";
			tmpStr += listItem.get(SHORTMSG_TIMES).toString() + "\r\n";
		}
		mResultText.setText(tmpStr);
	}
}